package com.factory.iamp.utils.helper;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class TableProblemHelper {
    private static HashMap<Integer, String> typeLists;

    TableProblemHelper() {
        typeLists = new HashMap<>();
        typeLists.put(1, "kazhi");
        typeLists.put(2, "fenhe");
        typeLists.put(3, "caigoupin");
        typeLists.put(4, "houshigu");
        typeLists.put(5, "diaoqi");
        typeLists.put(6, "texing");
        typeLists.put(7, "qita");
        typeLists.put(11, "卡滞");
        typeLists.put(21, "分合");
        typeLists.put(31, "采购品");
        typeLists.put(41, "后事故");
        typeLists.put(51, "掉漆");
        typeLists.put(61, "特性");
        typeLists.put(71, "其他");
    }

    static public String findProblemName(int pid) {
        return typeLists.get(pid);
    }

    static public Map<String, Integer> ProblemMap() {
        Map<String, Integer> result = new HashMap<>();
        for(String problem: typeLists.values()) result.put(problem, 0);
        return result;
    }

    public String fromJSON(JSONObject data) {
        if(data == null || data.keySet().size() < 1) return "";
//        StringBuilder result = new StringBuilder("问题记录：");
        StringBuilder result = new StringBuilder();
        List<Integer> idList = data.getObject("list", JSONArray.class).toJavaList(Integer.class);
        for(int i = 0;i < idList.size();i++) {
            if(i != 0) result.append(", ");
            result.append(typeLists.get(idList.get(i) * 10 + 1));
            if(idList.get(i) == 7) {
                result.append(": ");
                result.append(data.getObject("otherValue", String.class));
            }
        }
//        result.append("; 备注：");
        result.append("; ");
        result.append(data.getObject("commit", String.class));
        result.append(";");
        return result.toString();
    }
}
